<?php

class PracticeTestResultModel extends CoreModel {

	function __construct()
	{
		parent::__construct();
	}

	public function returnTable()
	{
		return $this->PracticeTestResults;
	}


	public function getCurrentDoing($id,$user)
	{
		return $this->wpdb->get_row("SELECT * FROM ".$this->returnTable()." WHERE practice_test_id=".$id." AND user_id='".$user."'");
	}

	public function countPracticeTestSuccess($user)
	{
		return $this->wpdb->query("SELECT id FROM ".$this->PracticeTestResults." WHERE progressbar=100 AND user_id='".$user."'");
	}

	public function calPointOfUser($user)
	{
		$this->loadModel('FaceBookLoginModel');
		$exists = $this->wpdb->get_var("SELECT id FROM ".$this->returnTable()." WHERE user_id='".$user."'");
		if(!empty($exists)){
			$totalPoint = $this->wpdb->get_var("SELECT SUM(accumulation_point) as user_point FROM ".$this->returnTable()." WHERE user_id='".$user."'");
			$this->FaceBookLoginModel->update(array(
				'accumulation_point'=>$totalPoint
			),array('user_id'=>$user));
		}
	}

	public function getPoints($id,$user)
	{
		return $this->wpdb->get_var("SELECT accumulation_point FROM ".$this->returnTable()." WHERE practice_test_id=".$id." AND user_id='".$user."'");
	}

	public function exists($id,$user)
	{
		$q = $this->wpdb->get_var("SELECT id FROM ".$this->returnTable()." WHERE practice_test_id=".$id." AND user_id='".$user."'");
		return !empty($q) ? true : false;
	}

	public function getTimerDoing($id,$user)
	{
		return $this->wpdb->get_var("SELECT time_stop FROM ".$this->returnTable()." WHERE practice_test_id=".$id." AND user_id='".$user."'");
	}

	public function getNumAnswer($id,$user)
	{
		return $this->wpdb->get_var("SELECT number_answer FROM ".$this->returnTable()." WHERE practice_test_id=".$id." AND user_id='".$user."'");
	}

	public function getNumberAnswerCorrect($id,$user)
	{
		return $this->wpdb->get_var("SELECT number_correct FROM ".$this->returnTable()." WHERE user_id='".$user."' AND practice_test_id=".$id);
	}

	public function getNumberAnswerWrong($id,$user)
	{
		return $this->wpdb->get_var("SELECT number_wrong FROM ".$this->returnTable()." WHERE user_id='".$user."' AND practice_test_id=".$id);
	}


}